package io.gitee.mingbaobaba.apijson.querycondition.configuration;

import io.gitee.mingbaobaba.apijson.querycondition.query.dao.DwQueryDao;
import io.gitee.mingbaobaba.apijson.querycondition.query.dao.impl.DefaultHttpApiJsonQueryDaoImpl;
import io.gitee.mingbaobaba.apijson.querycondition.query.dao.support.AccessTokenProvider;
import io.gitee.mingbaobaba.apijson.querycondition.query.dao.support.CachingAccessTokenProvider;
import io.gitee.mingbaobaba.apijson.querycondition.query.dao.support.OkHttpClientFactory;
import io.gitee.mingbaobaba.apijson.querycondition.query.properties.ApiJsonQueryProperties;
import io.gitee.mingbaobaba.apijson.querycondition.query.template.ApiJsonQueryTemplate;
import io.gitee.mingbaobaba.apijson.querycondition.query.template.support.LoopQueryExecutor;
import io.gitee.mingbaobaba.apijson.querycondition.query.template.mapping.ResultMapper;
import io.gitee.mingbaobaba.apijson.querycondition.query.template.mapping.FastjsonResultMapper;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * <p>自动配置</p>
 *
 * @author yingsheng.ye
 * @version 1.0.0
 * @since 2023/7/5 10:43
 */
@Slf4j
@Configuration
@ConditionalOnClass(ApiJsonQueryTemplate.class)
@EnableConfigurationProperties(ApiJsonQueryProperties.class)
@ComponentScan(basePackages = "io.gitee.mingbaobaba.apijson.querycondition.query")
public class AutoConfiguration {

    @Resource
    private ApiJsonQueryProperties dataWarehouseProperties;

    @PostConstruct
    public void postConstruct() {
        log.debug("ApiJson Configuration Start");
    }

    @Bean
    @ConditionalOnMissingBean
    public OkHttpClient apiJsonOkHttpClient() {
        return OkHttpClientFactory.create(dataWarehouseProperties);
    }

    @Bean
    @ConditionalOnMissingBean
    public AccessTokenProvider apiJsonAccessTokenProvider(OkHttpClient apiJsonOkHttpClient) {
        return new CachingAccessTokenProvider(dataWarehouseProperties, apiJsonOkHttpClient);
    }

    @Bean
    @ConditionalOnMissingBean
    public DwQueryDao apiJsonQueryDao(OkHttpClient apiJsonOkHttpClient,
                                      AccessTokenProvider apiJsonAccessTokenProvider) {
        return new DefaultHttpApiJsonQueryDaoImpl(dataWarehouseProperties, apiJsonOkHttpClient, apiJsonAccessTokenProvider);
    }

    @Bean
    @ConditionalOnMissingBean
    public LoopQueryExecutor apiJsonLoopQueryExecutor() {
        return new LoopQueryExecutor();
    }

    @Bean
    @ConditionalOnMissingBean
    public ResultMapper apiJsonResultMapper() {
        return new FastjsonResultMapper();
    }

    @Bean
    @ConditionalOnMissingBean
    public ApiJsonQueryTemplate apiJsonQueryTemplate(DwQueryDao apiJsonQueryDao,
                                                     LoopQueryExecutor apiJsonLoopQueryExecutor,
                                                     ResultMapper apiJsonResultMapper) {
        log.debug("加载默认ApiJson查询实现....");
        ApiJsonQueryTemplate apiJsonQueryTemplate = new ApiJsonQueryTemplate(apiJsonLoopQueryExecutor, apiJsonResultMapper);
        apiJsonQueryTemplate.setDao(apiJsonQueryDao);
        return apiJsonQueryTemplate;
    }


}
